import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;

public class MyArraylist implements mlist{
    public  int []elem;//数组
    public int size;//数组实际元素数目
    public MyArraylist(){
        this.elem=new int[10];
    }

    @Override
    public void add(int val) {
        if(IsFull()){
            exsize();
        }
        elem[size++]=val;
    }

    @Override
    public void add(int index, int val) {
        //index范围要合法
        if(index<0||index>elem.length||index-size>1){
            throw new RuntimeException("index范围不合法");
        }
        if(IsFull()){
            exsize();
        }
        //找到index插入val
        //插入val前需把index后的元素往后移一位
        for(int i=elem.length-1;i>index;i--){
           elem[i]=elem[i-1];
        }
        elem[index]=val;
        size++;
    }

    @Override
    public boolean IsFull() {
        return this.size==elem.length;
    }

    @Override
    public void exsize() {
        elem= Arrays.copyOf(elem,2*size);
    }

    @Override
    public boolean contains(int val) {
        if(getsize()<=0){
            throw new RuntimeException("数组为空");
        }
        for(int i=0;i<elem.length;i++){
            if(elem[i]==val){
                return true;
            }
        }
        return false;
    }

    @Override
    public int Indexof(int val) {
        if(getsize()<=0){
            throw new RuntimeException("数组为空");
        }
        for(int i=0;i<elem.length;i++){
            if(elem[i]==val){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int index) {
        if(getsize()<=0){
            throw new RuntimeException("数组为空");
        }
        if(index<0||index>elem.length){
            throw new RuntimeException("index不合法");
        }
        return elem[index];
    }

    @Override
    public void set(int index, int val) {
        if(getsize()<=0){
            throw new RuntimeException("数组为空");
        }
        if(index<0||index>elem.length){
            throw new RuntimeException("index不合法");
        }
        elem[index]=val;
    }

    @Override
    public int getsize() {
        return size;
    }

    @Override
    public void remove(int index) {
        if(index<0||index>elem.length){
            throw new RuntimeException("index不合法");
        }
        if(getsize()<=0){
            throw new RuntimeException("数组已为空");
        }
        for(int i=index;i<elem.length-1;i++){
            elem[i]=elem[i+1];
        }
        size--;
    }

    @Override
    public void clear() {
        size=0;
    }
}


class Test{
    public static void main(String[] args) {
        MyArraylist myArraylist = new MyArraylist();
        myArraylist.add(1);
        myArraylist.add(3);
        myArraylist.add(5);
        myArraylist.add(5);
        myArraylist.add(7);
        myArraylist.add(9);
        myArraylist.add(3,33);
        myArraylist.add(4,44);
        myArraylist.add(9);
        myArraylist.add(666);
        boolean find=myArraylist.contains(666);
        System.out.println(find);
        int find2=myArraylist.Indexof(666);
        System.out.println(find2);
        int get=myArraylist.get(6);
        System.out.println(get);
        myArraylist.remove(3);
        myArraylist.set(8,888);
        for(int data:myArraylist.elem){
            System.out.print(data+" ");
        }
        myArraylist.clear();

        new ArrayList<>();

    }
}
